package com.wxyzz.canteenadmin.shop.shopList.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxyzz.canteenadmin.shop.shopList.VO.ShopVO;
import com.wxyzz.canteenadmin.shop.shopList.mapper.ShopListMapper;
import com.wxyzz.canteenadmin.shop.shopList.param.ShopAddParam;
import com.wxyzz.canteenadmin.shop.shopList.param.ShopListPageParam;
import com.wxyzz.canteenadmin.shop.shopList.service.ShopListService;
import com.wxyzz.canteencommon.entity.shop.shopList.ShopList;
import com.wxyzz.canteencommon.exception.CustmerException;
import com.wxyzz.canteencommon.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * --- 加油，努力 ---
 *
 * @Author XYP
 * @Date 2024/10/10
 * @Solgne 从想做到能做
 * @Desc
 */
@Service
public class ShopListServiceImpl implements ShopListService {
    @Autowired
    private ShopListMapper shopListMapper;

    //分页和模糊查询
    @Override
    public PageInfo<ShopList> page(ShopListPageParam shopListPageParam) {
        PageHelper.startPage(shopListPageParam.getPageNum(), shopListPageParam.getPageSize());
        List<ShopList> shopList = shopListMapper.page(shopListPageParam.getName(),shopListPageParam.getStatus());
        PageInfo<ShopList> shopListPageInfo = new PageInfo<ShopList>(shopList);
        return shopListPageInfo;
    }

    @Override
    public ShopList findById(Integer id) {
        return shopListMapper.selectById(id);
    }

    @Override
    public void deleteById(Integer id) {
        shopListMapper.deleteById(id);
        RedisUtils.delKey("shops");
    }

    @Override
    public void deleteBatch(List<Integer> ids) {
        shopListMapper.deleteBatch(ids);
        RedisUtils.delKey("shops");
    }

    @Override
    public void addShop(ShopAddParam shopAddParam) {
        if (shopAddParam.getName()==null){
            throw new CustmerException(500,"店铺名称未填写...");
        }
        if (shopAddParam.getContacts()==null){
            throw new CustmerException(500,"联系人未填写...");
        }
        if (shopAddParam.getPhoneNumber()==null){
            throw new CustmerException(500,"手机号未填写...");
        }
        if (shopAddParam.getAddress()==null){
            throw new CustmerException(500,"地址未填写...");
        }
        shopListMapper.insert(shopAddParam);
        RedisUtils.delKey("shops");
    }

    @Override
    public void updateById(ShopList shopList) {
        if (shopList.getIsDefault() == 1) {
            List<ShopList> shopLists = shopListMapper.page(null, null);
            for (ShopList list : shopLists) {
                if (list.getIsDefault() == 1){
                    throw new CustmerException(500,"已存在默认店铺...");
                }
            }
        }
        shopListMapper.updateById(shopList);
    }

    @Override
    public List<ShopVO> findShops() {
        List<ShopVO> list = (List<ShopVO>)RedisUtils.getValue("shops");
        if (list == null || list.size() == 0) {
            System.out.println("通过数据库查询。。。");
            list = shopListMapper.selectShops();
            RedisUtils.setValueTimeout("shops", list, 24 * 3600);
        }
        return list;
    }
}
